<?php
class orderAfter extends userBase {
    function addRefundTask($id,$type){
        $order=OrderMode::init();
        $userId=$this->user->getLoginId();
        $orderInfo=$order->getDetailWithoutJoin($id);
        //检查一下订单状态
        switch($orderInfo['state']){
            case 'unshipped':$type=0;break;
            case 'shipped_direct':$type=1;break;
            case 'received':$type=1;break;
            case 'unused':$type=0;break;
            default:  echo "该订单当前状态不能申请退货/退款";return null;
        }
        $money=$orderInfo['payment_amount'];
        $r=$order->addRefund($id,$userId,$money,null,null,$type);
        if($r!=-1 && $order->setState($id,'refunding')){
            $result['message']="申请退款成功";
            $result['url']=WebRouter::init()->getPage('orderAfter','refund');
            View::displayAsHtml($result,"plugin/state/tips.php");
        }else{
            $result['message']="申请退款失败";
            View::displayAsHtml($result,"plugin/state/tips.php");
        }
    }
    function refundTask($page){
        $page= (int)((int)$page>0?$page:1);
        $countOfPage = 15;
        $offset=($page-1)*$countOfPage;
        $userId=$this->user->getLoginId();
        list($r['list'],$total)=OrderMode::init()->getRefundList($userId,null,$countOfPage,$offset);
        $url=WebRouter::init()->getQuestion("page=");
        $r['page']=new Page($page,$total,$countOfPage);
        $r['page']->setPageCallback(function($page)use($url){
            return $url.$page;
        });
        $r['pageEmpty']=$total==0?true:false;
        View::displayAsHtml($r, "order-refund.php");
    }
    function refundExpressTask($refund_id,$express_name,$express_num){
        if(empty($express_name)||empty($express_num)){
            $result['message']="物流信息不能为空哦~";
        }else{
            $setRE=OrderMode::init()->setRefundExpress($refund_id,$express_name,$express_num);
            if($setRE){
                $result['message']="退货信息填写成功";
            }else{
                $result['message']="退货信息填写失败，请重试";
            }
        }
        $result['url']=WebRouter::init()->getPage('orderAfter','refund');
        View::displayAsHtml($result,"plugin/state/tips.php");
    }
    //评价订单
    function commentTask($id){
        $o = OrderMode::init();
        $userId=$this->user->getLoginId();
        $valid=$o->checkOrderBelongsToUser($id,$userId);
        if(empty($id)||!$valid){
            echo "非法操作";
            return null;
        }
        $id=(int)$id;
        $r=$o->getDetail($id);
        View::displayAsHtml($r,'order-comment.php');
    }
     //直接评价一个大宗商品！
    function addCommentDirectTask($user_id,$gopen_id,$comment,$score,$goods_photo){
        //var_dump($goods_photo);
        if(!empty($goods_photo)){
            $pic_ids_string = "";
            foreach ($goods_photo as $key => $value) {
                $pic_ids_string .= (int)$value;
                if(count($goods_photo) != $key) $pic_ids_string .= ",";
            }
        }else{
            $pic_ids_string = null;
        }
        //var_dump($pic_ids_string);
        $result=OrderMode::init()->addComment(-1,$user_id,$gopen_id,$comment,$score,$pic_ids_string,'special');
        if($result){
            $r['message']="评论成功";
        }else{
            $r['message']="评论失败";
        }
        View::displayAsHtml($r,"plugin/state/tips.php");
    }
    function commentSubmitTask($order_id,$service_id,$goods_star,$goods_info,$service_star,$service_text,$goods_photo){
        $order_id = (int)$order_id;
        $o = OrderMode::init();
        $userId=$this->user->getLoginId();
        $valid=$o->checkOrderBelongsToUser($order_id,$userId);
        if(empty($order_id)||!$valid){
            echo "非法操作";
            return null;
        }
        $goodsC=CommentModule::init('goods_comment','user_id','gopen_id');
        $serviceC=CommentModule::init('service_comment','order_id','service_id');
        $goodsInOrderIds=array();
        foreach ($goods_star as $k1=>$v1) {
            $goodsInOrderIds[$k1]['star']=$v1;
        }
        foreach ($goods_info as $k2=>$v2) {
            $goodsInOrderIds[$k2]['info']=$v2;
        }
        if(!empty($goods_photo)){
            foreach ($goods_photo as $k3=>$v3) {
                $goodsInOrderIds[$k3]['photo']=implode(',',$v3);
            }
        }
        if(!empty($service_id)){
            $addService=$serviceC->add($order_id,$service_id,$service_text,$service_star);
        }else{
            $addService=1;
        }
        $addCI = true;
        foreach ($goodsInOrderIds as $k4=>$v4) {
            $goods_id=$o->getGoodsIdByGoodsInOrderId($k4);
            $gopen_id=GoodsMode::init()->getGopenId($goods_id);
            $photo=isset($v4['photo'])?$v4['photo']:'';
            $addGoodsId=$goodsC->add($userId,$gopen_id,$v4['info'],$v4['star'],$photo);
            if($addGoodsId>0){
                $thisAddCI = $o->addCommentId($k4,$addGoodsId);
                if($thisAddCI){
                    GoodsMode::init()->modifyCommentAndScore($goods_id,$v4['star'],1);
                }
                $addCI &=$thisAddCI;
            }
        }
        if($addService>0&&$addCI){
            $o->setState($order_id,'evaluate');
            $result['message']="评论成功";
        }else{
            $result['message']="评论失败";
        }
        $result['url']=WebRouter::init()->getPage('order','detail',array('id'=>$order_id));
        View::displayAsHtml($result,"plugin/state/tips.php");
    }
    //确认收货
    function confirmTask($id){
        $o = OrderMode::init();
        $userId=$this->user->getLoginId();
        $valid=$o->checkOrderBelongsToUser($id,$userId);
        if(empty($id)||!$valid){
            echo "非法操作";
            return null;
        }
        $id=(int)$id;
        $r=$o->setState($id,'confirmed');
        //修改销量
        $goods=OrderMode::init()->getGoodsInOrder($id);
        if(!empty($goods)){
            foreach ($goods as $v) {
                $a=GoodsMode::init()->modifySale($v['gopen_id'],$v['number']);
            }
        }
        $result['message']=$r?"订单确认成功":"订单确认失败";
        $result['url']=WebRouter::init()->getPage('order','detail',array('id'=>$id));
        View::displayAsHtml($result,"plugin/state/tips.php");
    }
    function serviceCodeTask($id){
        $o = OrderMode::init();
        $userId=$this->user->getLoginId();
        $valid=$o->checkOrderBelongsToUser($id,$userId);
        if(empty($id)||!$valid){
            echo "非法操作";
            return null;
        }
        $juhe=JuheSMSMode::init();
        $userInfo = $this->user->getLoginInfo();
        $tel = $userInfo['username'];
        $type='service';
        $code = $o->getServiceCode($id);
        if(empty($code)){
            $code=$this->user->getCode($tel);
            $juheRe=$juhe->send($tel, $type, array("#name#"=>$tel,"#code#"=>$code));
            $re=new DataMessage($juheRe['error_code']==0? DataMessage::STATE_SUCCESS: DataMessage::STATE_ERROR, $juheRe['reason']);
            if($re->state == DataMessage::STATE_SUCCESS){
                $o->insertCode($id,$code);
                $result['message']="验证码发送成功";
            }else{
                $result['message']="验证码发送失败";
            }
        }else{
            $result['message']="您该商品的验证码是：".$code;
        }
        View::displayAsHtml($result,"plugin/state/tips.php");

    }
}